/*! mobile-select-area author:tianxiangbing email:55342775@qq.com
 * demo:http://www.lovewebgames.com/jsmodule/mobile-select-area.html 
 * git:https://github.com/tianxiangbing/mobile-select-area  2017-07-12 */
! function(a, b) {
	"function" == typeof define && define.amd ? define(["jquery", "dialog"], b) : "function" == typeof define && define.cmd ? define(function(a, c, d) {
		var e = a("jquery"),
			f = a("dialog");
		return b(e, f)
	}) : "object" == typeof exports ? module.exports = b() : a.MobileSelectArea = b(jQuery || Zepto)
}(this, function(a, b) {
	var c = function() {
		var a = Math.random().toString().replace(".", "");
		this.id = "scroller_" + a, this.scroller, this.data, this.index = 0, this.value = [0, 0, 0], this.oldvalue, this.oldtext = [], this.text = ["", "", ""], this.level = 3, this.mtop = 30, this.separator = " "
	};
	return c.prototype = {
		init: function(b) { 
			this.settings = a.extend({
				eventName: "click",
				autoHide: !0
			}, b), this.trigger = a(this.settings.trigger), void 0 == this.settings.default ? this.default = 1 : this.default = 0, level = parseInt(this.settings.level), this.level = level > 0 ? level : 3, this.trigger.attr("readonly", "readonly"), this.value = this.settings.value && this.settings.value.split(",") || [0, 0, 0], this.text = this.settings.text || this.trigger.val().split(" ") || ["", "", ""], this.oldvalue = this.value.concat([]), this.oldtext = this.text.concat([]), this.clientHeight = document.documentElement.clientHeight || document.body.clientHeight, this.clientWidth = document.documentElement.clientWidth || document.body.clientWidth, this.bindEvent()
		}, 
		getData: function() {
			var b = this,
				c = a.Deferred();
			return "object" == typeof this.settings.data ? (this.data = this.settings.data, c.resolve()) : a.ajax({
				dataType: "json",
				cache: !0,
				url: this.settings.data,
				type: "GET",
				success: function(a) {
					b.data = a.data, c.resolve()
				},
				accepts: {
					json: "application/json, text/javascript, */*; q=0.01"
				}
			}), c
		},
		show: function() {
			for(var b = this, c = "", d = 0; d < b.level; d++) c += "<div></div>";
			var e, f;
			if("bottom" == b.settings.position) {
				e = {
					position: "bottom",
					width: "100%",
					className: "ui-dialog-bottom",
					animate: !1
				};
				var f = [{
					no: "取消"
				}, {
					yes: "确定"
				}]
			}
			a.confirm('<div class="ui-scroller-mask"><div id="' + b.id + '" class="ui-scroller">' + c + "<p></p></div></div>", f, function(a, c) {
				"yes" == a && b.submit(), "no" == a && b.cancel(), this.dispose()
			}, a.extend({
				width: 320,
				height: 215
			}, e)), b.settings.autoHide && a(".ui-dialog-mask").on("click", function() {
				a(".ui-confirm-no").trigger("click")
			}), b.scroller = a("#" + b.id), b.getData().done(function() {
				b.format()
			});
			var g = 0,
				h = 0;
			b.scroller.children().bind("touchstart", function(a) {
				g = (a.changedTouches || a.originalEvent.changedTouches)[0].pageY
			}), b.scroller.children().bind("touchmove", function(b) {
				h = (b.changedTouches || b.originalEvent.changedTouches)[0].pageY;
				var c = h - g,
					d = a(b.target).parent();
				if("DL" == d[0].nodeName) {
					var e = parseInt(d.css("top") || 0) + c;
					return d.css("top", e), g = h, !1
				}
			}), b.scroller.children().bind("touchend", function(c) {
				h = (c.changedTouches || c.originalEvent.changedTouches)[0].pageY;
				var d = h - g,
					e = a(c.target).parent();
				if("DL" == e[0].nodeName) {
					var f = a(e.parent()).index(),
						i = parseInt(e.css("top") || 0) + d;
					i > b.mtop && (i = b.mtop), i < -a(e).height() + 60 && (i = -a(e).height() + 60);
					var j = i / b.mtop,
						k = Math.round(j),
						l = Math.abs(k) + 1;
					if(1 == k && (l = 0), b.value[f] = a(e.children().get(l)).attr("ref"), 0 == b.value[f] ? b.text[f] = "" : b.text[f] = a(e.children().get(l)).html(), !a(e.children().get(l)).hasClass("focus")) {
						for(var m = b.level - 1; m > f; m--) b.value[m] = 0, b.text[m] = "";
						b.format()
					}
					return a(e.children().get(l)).addClass("focus").siblings().removeClass("focus"), e.css("top", k * b.mtop), !1
				}
			})
		},
		bindEvent: function() {
			var a = this;
			this.trigger[a.settings.eventName](function(b) {
				return a.show(), !1
			})
		},
		format: function() {
			var a = this;
			a.scroller.children();
			this.f(this.data), console.log(a.text)
			localStorage.setItem("province",a.text[0])
			localStorage.setItem("city",a.text[1])
			localStorage.setItem("district",a.text[2])
//			var province = a.text[0]
//			var city = a.text[1]
//			var district = a.text[2]
		},
		f: function(b) {
			var c = this,
				d = b;
			d || (d = []);
			var e, f = '<dl><dd ref="0">——</dd>',
				g = 0,
				h = c.mtop;
			if(0 !== c.index && "0" == c.value[c.index - 1] && 0 == this.default) f = '<dl><dd ref="0" class="focus">——</dd>', c.value[c.index] = 0, c.text[c.index] = "", g = 0;
			else {
				if("0" == c.value[c.index] && (f = '<dl><dd ref="0" class="focus">——</dd>', g = 0), d.length > 0 && 1 == this.default) {
					f = "<dl>";
					var i = d[0].pid || 0,
						j = d[0].id || 0;
					g = d[0].id, e = d[0].child, c.value[this.index] || (c.value[this.index] = j, c.text[this.index] = d[0].name), f += '<dd pid="' + i + '" class="' + m + '" ref="' + j + '">' + d[0].name + "</dd>"
				}
				for(var k = c.default, l = d.length; k < l; k++) {
					var i = d[k].pid || 0,
						j = d[k].id || 0,
						m = "";
					c.value[c.index] == j && (m = "focus", g = j, e = d[k].child, h = c.mtop * -(k - c.default)), f += '<dd pid="' + i + '" class="' + m + '" ref="' + j + '">' + d[k].name + "</dd>"
				}
			}
			f += "</dl>";
			var n = a(f);
			n.css("top", h);
			var o = c.scroller.children();
			return a(o[c.index]).html(n), c.index++, c.index > c.level - 1 ? void(c.index = 0) : void c.f(e)
		},
		submit: function() {
//			localStorage.setItem("a",b)s
			this.oldvalue = this.value.concat([]), this.oldtext = this.text.concat([]), 1 == this.trigger[0].nodeType && (this.trigger.val(this.text.join(this.separator)), this.trigger.attr("data-value", this.value.join(","))), this.trigger.next(":hidden").val(this.value.join(",")), this.settings.callback && this.settings.callback.call(this, this.scroller, this.text, this.value)
		},
		cancel: function() {
			this.value = this.oldvalue.concat([]), this.text = this.oldtext.concat([])
		}
	}, c
});